﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using XRGLib;
using Console = XRGLib.Console;
using Microsoft.Xna.Framework.Input;

namespace Examples
{
    class Example5 : ExampleBase
    {
        Surface menuSurface;
        Point start, end;

        SpatialAStar<Cell, object> pather;

        public Example5(Console console, int width, int height, Game game)
            : base(console, width, height, game)
        {
            menuSurface = console.CreateSurface(50, 6);

            start = Point.Zero;
            end = new Point(width / 2 , height / 2);

            pather = new SpatialAStar<Cell, object>(map);
        }

        public override string Description
        {
            get { return "Pathfinding"; }
        }

        public override void OnActivated()
        {
            game.IsMouseVisible = true;
        }

        public override void OnDeactivated()
        {
            game.IsMouseVisible = false;
        }

        public override void OnKeyboard(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Space:
                    MakeMap();
                    
                    break;

            }
        }

        public override void OnMouseButton(MouseButton button, int cx, int cy)
        {
            if (button == MouseButton.Left)
            {
                start = new Point(cx, cy);
            }
            else if(button == MouseButton.Right)
            {
                end = new Point(cx, cy);
            }
        }

        public override Surface ReDraw()
        {
            surface.Clear();

            DrawMap();
            DrawPath();

            DrawMenu();

            console.BlitAlpha(menuSurface, surface, menuSurface.Rect,
                (surface.Width - menuSurface.Width) / 2,
                surface.Height - menuSurface.Height - 1,
                0.9f);

            return base.ReDraw();
        }

        void DrawPath()
        {
            var path = pather.Search(start, end, null);
            if (path == null)
                return;

            foreach (var n in path)
            {
                surface.SetBackground(n.X, n.Y, Color.Red);
            }
        }

        void DrawMenu()
        {
            menuSurface.DefaultBackground = Color.Beige;
            menuSurface.DefaultForeground = Color.Black;

            menuSurface.DrawFrame(menuSurface.Rect, null, true);

            menuSurface.PrintString(1, 1, "Left click to place Start Position");
            menuSurface.PrintString(1, 2, "Left click to place End Position");
            menuSurface.PrintString(1, 3, "[SPACE] New Random Map");
        }

        void DrawMap()
        {
            for (int y = 0; y < map.Height; y++)
            {
                for (int x = 0; x < map.Width; x++)
                {
                    if (map[x, y].IsWall)
                        surface.PrintChar(x, y, '#');
                    else
                        surface.PrintChar(x, y, '.');
                }
            }
        }
    }
}
